(function($) {
    var $document = $(document);
    
    $document.on({
        'ajax:beforeSend': function() {
            $.utils.clearFieldError('form-signup-account');
            $(this).find('.btn').attr('disabled', 'disabled');
            $('#form-user-register .error-datetime').text('');
        },
        'ajax:success': function(e, data) {
            if (data.status) {
                window.location.href = data.redirect;
            } else {
                if (data.code === 'invalid_data') {
                    for (var field in data.messages) {
                    	$.utils.showFieldError('form-signup-account', field, data.messages[field][0]);
                    }
                    $.utils.autoFocus('form-signup-account', '.input-error');
                }
            }
        },
        'ajax:complete': function() {
            $(this).find('.btn').removeAttr('disabled');
        }

    }, '#form-signup-account');
    
    
    
    /**
     * User login (normal)
     */
    
    $document.on({
        'ajax:beforeSend': function() {
            $.utils.clearFieldError('form-user-login');
            $(this).find('.btn').attr('disabled', 'disabled');
        },
        'ajax:success': function(e, data) {
            if (data.status) {
                //window.location.href = data.redirect;
            	//window.location.reload();
            	if (typeof data.redirect != '') {
            		window.location.href = data.redirect;
            	} else {
            		window.location.reload();
            	}
            	
            } else {
                if (data.code === 'invalid_data') {
                    for (var field in data.messages) {
                        $.utils.showFieldError('form-user-login', field, data.messages[field][0]);
                    }
                    $.utils.autoFocus('form-user-login', '.input-error');
                    $('#loginerror').text('');
                }
                else {
                    $('#loginerror').text(data.message);
                }
                
            }
        },
        'ajax:complete': function() {
            $(this).find('.btn').removeAttr('disabled');
        }

    }, '#form-user-login');
    
    
    
})(jQuery);